/* Copyright (c) 2010 OFXKit
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "OFXCreditCardRequest.h"

#import "OFXAccountInfoRequest.h"
#import "OFXCreditCardMessageSet.h"
#import "OFXSignUpMessageSet.h"
#import "OFXSignOnRequest.h"
#import "OFXStatementRequest.h"

@implementation OFXCreditCardRequest

+ (id) accountInfoForInstitution:(OFXFinancialInstitution*)institution
                         withURL:(NSURL*)url
                        username:(NSString*)user
                        password:(NSString*)pass
{
  OFXApplication* app = [[OFXApplication alloc] init];
  OFXCreditCardRequest* creditCardRequest = 
    [OFXCreditCardRequest accountInfoForInstitution:institution 
                                            withURL:url
                                           username:user 
                                           password:pass
                                     ofxApplication:app];
  return creditCardRequest;
}

+ (id) accountInfoForInstitution:(OFXFinancialInstitution*)institution
                         withURL:url
                        username:(NSString*)user
                        password:(NSString*)pass
                  ofxApplication:(OFXApplication*)app
{
  OFXCreditCardRequest* creditCardRequest = 
    [[OFXCreditCardRequest alloc] init];
  
  OFXDocumentRequest* request = [[OFXDocumentRequest alloc] 
                                 initWithVersion:@"103"];
  OFXSignOnMessageSet* signOnMessageSet = [[OFXSignOnMessageSet alloc] init];
  OFXSignOnRequest* signOnRequest = [[OFXSignOnRequest alloc] init];
  [signOnRequest setUserId:user];
  [signOnRequest setUserPassword:pass];
  [signOnRequest setOfxApplication:app];
  
  [signOnRequest setFinancialInstitution:institution];
  [signOnMessageSet setSignOnRequest:signOnRequest];  
  [request setSignOnRequest:signOnMessageSet];
  
  OFXSignUpMessageSet* msgSet = [[OFXSignUpMessageSet alloc] init];
  OFXAccountInfoRequest* accountInfoRequest = [[OFXAccountInfoRequest alloc] 
                                               init];
  
  [[msgSet accountInformationRequests] addObject:accountInfoRequest];
  [[request messageSets] addObject:msgSet];
  
  NSError* error;
  [creditCardRequest sendRequest:request
                         withURL:url
                           error:&error];
  
  return creditCardRequest;
}

+ (id) transactionsForAccount:(OFXAccount*)account
                atInstitution:(OFXFinancialInstitution*)institution
                      withURL:url
                     username:(NSString*)user
                     password:(NSString*)pass
{
  OFXCreditCardRequest* creditCardRequest =  
    [OFXCreditCardRequest transactionsForAccount:account
                                   atInstitution:institution 
                                         withURL:url
                                        username:user 
                                        password:pass
                                  ofxApplication:[[OFXApplication alloc] init]];
  return creditCardRequest;
}

+ (id) transactionsForAccount:(OFXAccount*)account
                atInstitution:(OFXFinancialInstitution*)institution
                      withURL:url
                     username:(NSString*)user
                     password:(NSString*)pass
               ofxApplication:(OFXApplication*)app
{
  // get transactions from 90 days in the past.
  NSDate* start = [NSDate dateWithTimeIntervalSinceNow:-3600*24*90];
  NSDate* end   = [NSDate dateWithTimeIntervalSinceNow:0];
  
  OFXCreditCardRequest* creditCardRequest = 
    [OFXCreditCardRequest transactionsBetweenDate:start 
                                          andDate:end
                                      withAccount:account
                                    atInstitution:institution
                                          withURL:url
                                         username:user
                                         password:pass
                                   ofxApplication:app];
  
  return creditCardRequest;  
}

+ (id) transactionsBetweenDate:(NSDate*)start
                       andDate:(NSDate*)end
                   withAccount:(OFXAccount*)account
                 atInstitution:(OFXFinancialInstitution*)institution
                       withURL:url
                      username:(NSString*)user
                      password:(NSString*)pass
{
  OFXApplication* app = [[OFXApplication alloc] init];
  OFXCreditCardRequest* creditCardRequest =
    [OFXCreditCardRequest transactionsBetweenDate:start
                                          andDate:end
                                      withAccount:account
                                    atInstitution:institution
                                          withURL:url
                                         username:user
                                         password:pass
                                   ofxApplication:app];
  
  return creditCardRequest;
}

+ (id) transactionsBetweenDate:(NSDate*)start
                       andDate:(NSDate*)end
                   withAccount:(OFXAccount*)account
                 atInstitution:(OFXFinancialInstitution*)institution
                       withURL:url
                      username:(NSString*)user
                      password:(NSString*)pass
                ofxApplication:(OFXApplication*)app
{
  OFXCreditCardRequest* creditCardRequest = 
    [[OFXCreditCardRequest alloc] init];
  
  OFXDocumentRequest* request = [[OFXDocumentRequest alloc] 
                                 initWithVersion:@"103"];
  OFXSignOnMessageSet* signOnMessageSet = [[OFXSignOnMessageSet alloc] init];
  OFXSignOnRequest* signOnRequest = [[OFXSignOnRequest alloc] init];
  [signOnRequest setUserId:user];
  [signOnRequest setUserPassword:pass];
  [signOnRequest setOfxApplication:app];
  
  [signOnRequest setFinancialInstitution:institution];
  [signOnMessageSet setSignOnRequest:signOnRequest];  
  [request setSignOnRequest:signOnMessageSet];
  
  OFXCreditCardMessageSet* msgSet = [[OFXCreditCardMessageSet alloc] init];
  OFXStatementRequest* statementRequest = [[OFXStatementRequest alloc] init];
  
  [statementRequest setAccount:account];
  [statementRequest setDateStart:start];
  [statementRequest setDateEnd:end];
  [statementRequest setIncludeTransactions:YES];
  
  [[msgSet statementRequests] addObject:statementRequest];
  [[request messageSets] addObject:msgSet];
  
  NSError* error;
  [creditCardRequest sendRequest:request
                         withURL:url
                           error:&error];
  
  return creditCardRequest;
}

@end
